<?PHP

  class FileCache extends Cache{
    public static function setData($area, $key, $value){
      $file = self::getFileName($area, $key);
      $destination=fopen($file,"w");
      fwrite($destination,serialize($value));
      fclose($destination);
      chmod($file,0755);
    }

    public static function getData($area, $key){
      $source=fopen(self::getFileName($area, $key),"r");
      $code = "";
      while ($a=fread($source,1024)){
        $code.= $a;
      }
      return unserialize($code);
    }

    public static function contains($area, $key){
      return file_exists(self::getFileName($area, $key));
    }

    public static function clear($area = "",$key = ""){
      $path = self::getCacheDir($area);
      if($key == ""){
        FileSystem::deleteDir($path);
      }
      else{
        FileSystem::deleteFile($path."/".md5($key).".txt");
      }
    }
    
    public static function getCacheDir($area){
      $dir = Settings::getInstance()->get("root")."/content/cache/".$area."/";
      if(!file_exists(Settings::getInstance()->get("root")."/content/cache/")){
        mkdir(Settings::getInstance()->get("root")."/content/cache/");
      }
      if(!file_exists($dir)){
        mkdir($dir);
      }
      return $dir;
    }

    protected static function getFileName($area, $key){
      return self::getCacheDir($area).md5($key).".txt";
    }
  }

?>